package com.ymt.fmd.util;

import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;

public class HttpClientUtils {

	/**
	 * Logger
	 */
	private static Logger log = Logger.getLogger(HttpClientUtils.class);

	/*
	 * (1) GET方法 使用 HttpClient 需要以下 6 个步骤： 1. 创建 HttpClient 的实例 2. 创建某种连接方法的实例，在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址 3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例 4. 读
	 * response 5. 释放连接。无论执行方法是否成功，都必须释放连接 6. 对得到后的内容进行处理
	 */
	public static String executeByGET(String url, String username, String password, String mimeType, JSONObject parameters) {

		String str = null;
		// 创建默认的httpClient实例
		CloseableHttpClient httpClient = getHttpClient();

		try {
			// 创建Get方法实例
			HttpGet httpGets = new HttpGet(url);
			CloseableHttpResponse httpResponse = null;
			// 执行get请求
			httpResponse = httpClient.execute(httpGets);

			try {
				// 获取响应消息实体
				HttpEntity entity = httpResponse.getEntity();
				if (null != entity) {
					str = EntityUtils.toString(entity, "utf-8");
				}
			} finally {
				httpResponse.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				closeHttpClient(httpClient);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return str;
	}

	public static String executeByPOST(String url, String username, String password, String mimeType, Map<String, String> parameters) {

		String str = null;
		UrlEncodedFormEntity uefEntity = null; // 参数实体
		CloseableHttpResponse response = null;
		HttpEntity entity = null; // 响应实体
		// 创建HttpClient实例
		// CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpClient httpClient = getHttpClient();
		// 创建Post方法实例
		HttpPost httpPosts = new HttpPost(url);
		// 添加所需要的post内容
		List<NameValuePair> list = new ArrayList<NameValuePair>();

		Set<String> keySet = parameters.keySet();
		for (String key : keySet) {
			list.add(new BasicNameValuePair(key, parameters.get(key)));
		}

		try {
			// url格式编码
			uefEntity = new UrlEncodedFormEntity(list, "UTF-8"); // 设置httppost form实体为utf-8

			httpPosts.setEntity(uefEntity);
			// 执行post请求
			response = httpClient.execute(httpPosts);
			// 获取响应消息实体
			entity = response.getEntity();

			str = EntityUtils.toString(entity, "utf-8");

			if (null != entity) {
				log.info("-------------------------------------------------------");
				log.info(str);
				log.info("-------------------------------------------------------");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				closeHttpClient(httpClient);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return str;
	}

	private static CloseableHttpClient getHttpClient() {
		return HttpClients.createDefault();
	}

	private static void closeHttpClient(CloseableHttpClient client) throws IOException {
		if (client != null) {
			client.close();
		}
	}

	public static void main(String[] args) throws Exception {
		// executeByGET("http://localhost:8080/dummycbservice/portal/department","","","",null);
		executeByGET("http://localhost:8080/shmj/rest/test/test/中文", "", "", "", null);

		Map<String, String> params = new HashMap<String, String>();
		params.put("username", "admin");
		params.put("pwd", "1");
		executeByPOST("http://localhost:8080/shmj/rest/test/login", "", "", "", params);
	}

}
